package com.wbcs.config.component.application.report.condition;

import java.util.List;

import com.wbcs.config.component.application.report.ConditionBean;
import com.wbcs.config.component.application.report.SqlBean;
import com.wbcs.config.component.application.report.ReportDataSetBean;
import com.wbcs.exception.WbcsConfigLoadingException;
import com.wbcs.exception.WbcsRuntimeException;
import com.wbcs.system.ReportRequest;
import com.wbcs.system.assistant.ReportAssistant;
import com.wbcs.system.datatype.IDataType;
import com.wbcs.util.Tools;

public class ConditionInSqlBean implements Cloneable
{
    private String conditionname;
    
    private String placeholder;
    
    private ConditionExpressionBean conditionExpression;
    
    private ReportDataSetBean owner;
    
    public ConditionInSqlBean(ReportDataSetBean owner)
    {
        this.owner=owner;
    }
    
    public String getConditionname()
    {
        return conditionname;
    }

    public String getRealConditionname()
    {
        if(conditionname==null||conditionname.trim().equals("")||conditionname.equals("{#condition#}"))
        {
            return null;
        }
        if(conditionname.startsWith("{#")&&conditionname.endsWith("#}"))
        {
            return conditionname.substring(2,conditionname.length()-2);
        }
        if(conditionname.startsWith("#")&&conditionname.endsWith("#"))
        {
            return conditionname.substring(1,conditionname.length()-1);
        }
        return conditionname;
    }
    
    public void setConditionname(String conditionname)
    {
        this.conditionname=conditionname;
    }

    public String getPlaceholder()
    {
        return placeholder;
    }

    public void setPlaceholder(String placeholder)
    {
        this.placeholder=placeholder;
    }

    public ConditionExpressionBean getConditionExpression()
    {
        return conditionExpression;
    }

    public void setConditionExpression(ConditionExpressionBean conditionExpression)
    {
        this.conditionExpression=conditionExpression;
    }

    public String parseConditionInSql(ReportRequest rrequest,String sql,List<String> lstConditionValues,
            List<IDataType> lstConditionTypes)
    {
        String conname=this.getRealConditionname();
        if(conname==null||conname.trim().equals(""))
        {
            if(conditionname.equals("{#condition#}"))
            {
                return ReportAssistant.getInstance().addDynamicConditionExpressionsToSql(rrequest,this.owner.getReportBean(),this.owner,sql,
                        ((SqlBean)owner.getParent()).getLstConditions(),lstConditionValues,lstConditionTypes);
            }
            throw new WbcsRuntimeException("报表"+this.owner.getReportBean().getPath()+"中ConditionBeanInSqlBean的conditionname属性为空");
        }
        ConditionBean cbeanRefered=((SqlBean)owner.getParent()).getConditionBeanByName(conname);//取到被此条件引用的<condition/>
        String conditionvalue=cbeanRefered.getDynamicConditionvalueForSql(rrequest,-1);
        if(!conditionvalue.equals("")||(conditionname.startsWith("#")&&conditionname.endsWith("#")))
        {
            if(conditionname.startsWith("{#")&&conditionname.endsWith("#}"))
            {
                return Tools.replaceAll(sql,this.placeholder,conditionvalue);
            }
            return Tools.replaceAll(sql,this.placeholder,conditionExpression.getRuntimeConditionExpressionValue(cbeanRefered,conditionvalue,
                    lstConditionValues,lstConditionTypes));
        }else
        {
            return ReportAssistant.getInstance().removeConditionPlaceHolderFromSql(this.owner.getReportBean(),sql,this.placeholder);
        }
    }

    @Override
	public ConditionInSqlBean clone() 
    {
        try
        {
            ConditionInSqlBean newBean=(ConditionInSqlBean)super.clone();
            if(this.conditionExpression!=null)
            {
                newBean.setConditionExpression(conditionExpression.clone());
            }
            return newBean;
        }catch(CloneNotSupportedException e)
        {
            throw new WbcsConfigLoadingException("clone ConditionInSqlBean对象失败",e);
        }
    }
}

